import re
import jenkins


from conf.operationConfig import OperationConfig

conf = OperationConfig()


class OperJenkins:


    def __init__(self):
        self.__config = {
            'url': conf.get_section_jenkins('url'),
            'username': conf.get_section_jenkins('username'),
            'password': conf.get_section_jenkins('password'),
            'timeout': int(conf.get_section_jenkins('timeout'))
        }

        self.__server = jenkins.Jenkins(**self.__config)
        # ��ȡ��Jenkins�ϵ���Ŀ��
        self.job_name = conf.get_section_jenkins('job_name')

    def get_job_number(self):

        build_number = self.__server.get_job_info(self.job_name).get('lastBuild').get('number')
        return build_number

    def get_build_job_status(self):

        build_num = self.get_job_number()
        job_status = self.__server.get_build_info(self.job_name, build_num).get('result')
        return job_status

    def get_console_log(self):

        console_log = self.__server.get_build_console_output(self.job_name, self.get_job_number())
        return console_log

    def get_job_description(self):

        description = self.__server.get_job_info(self.job_name).get('description')
        url = self.__server.get_job_info(self.job_name).get('url')

        return description, url

    def get_build_report(self):

        report = self.__server.get_build_test_report(self.job_name, self.get_job_number())
        return report

    # def report_success_or_fail(self):
    #     """ͳ�Ʋ��Ա��������ɹ�����ʧ�������������Լ��ɹ��ʡ�ʧ����"""
    #     report_info = self.get_build_report()
    #     # ��ȡ���Ա�������
    #     console_log = self.get_console_log()
    #     report_line = re.search(r'http://192.168.****/job/jjapi/(.*?)allure', console_log).group(0)
    #
    #     return report_line

if __name__ == '__main__':
    des=OperJenkins()
    numbers=des.get_job_number()
    print(numbers)
    #logs=des.get_console_log()
    #print(logs)

